package com.stormrage.edu.serv.exam.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.exam.ExmAnswerDao;
import com.stormrage.edu.serv.exam.ExmAnswerServ;
import com.stormrage.entity.exam.ExmAnswer;
import com.stormrage.entity.exam.rep.ExmAnswerRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: ExmExamServImpl.java
 * @Package com.stormrage.edu.serv.exam.impl
 * @Description: 考试试题答案 Service
 * @author www.stormragetech.com
 * @date 2016年4月24日
 * @version V1.0
 */
@Service
public class ExmAnswerServImpl implements ExmAnswerServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmAnswerServImpl.class);

	@Autowired
	private ExmAnswerDao exmExamAnswerDao;

	@Override
	public Integer create(final ExmAnswer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 create 】-【 STARTED 】");

		Integer index = exmExamAnswerDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void update(final ExmAnswer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 update 】-【 STARTED 】");

		exmExamAnswerDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 update 】-【 COMPLETED 】");

	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 delete 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		exmExamAnswerDao.delete(map);

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 delete 】-【 COMPLETED 】");

	}

	@Override
	public Integer queryCountByExamId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryCountByExamId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("examId", arg0);

		Integer count = exmExamAnswerDao.queryCountByExamId(map);

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryCountByExamId 】-【 COMPLETED 】");
		return count;

	}

	@Override
	public ExmAnswer queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		ExmAnswer entity = exmExamAnswerDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<ExmAnswerRep> queryRepListByExamId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryRepListByExamId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("examId", arg0);

		List<ExmAnswer> array = exmExamAnswerDao.queryListByExamId(map);

		List<ExmAnswerRep> list = new ArrayList<ExmAnswerRep>();
		int seq = 1;

		Iterator<ExmAnswer> it = array.iterator();
		while (it.hasNext()) {
			ExmAnswer answer = it.next();
			ExmAnswerRep rep = ExmAnswerRep.instance(answer, seq);
			list.add(rep);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 ExmExamAnswerServ 】- 【 queryRepListByExamId 】-【 COMPLETED 】");
		return list;

	}

}
